package com.serkol.component;

import com.serkol.constant.AuthConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import java.util.Locale;

@Component
public class LocalUtil {

    private static MessageSource messageSource;

    public LocalUtil(MessageSource messageSource) {
        LocalUtil.messageSource = messageSource;
    }

    /**
     * 获取单个国际化翻译值
     */
    public static String get(String msgKey) {
        try {
            Locale locale = LocaleContextHolder.getLocale();
            String language = AuthConstant.getLanguage();
            if(StringUtils.isNotEmpty(language)){
               // zh-UG
                String[] split = language.split("-");
                locale=new Locale(split[0], split[1]);
            }
            System.out.println("LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL");
            System.out.println(language);
            return messageSource.getMessage(msgKey, null, locale);
        } catch (Exception e) {
            return msgKey;
        }
    }
}
